﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 圆形按钮
    /// </summary>
    public partial class ToggleSwitch
    {
        private static void OToggleSwitchPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ToggleSwitch)?.Refresh();
        }

        #region IsOn

        public static readonly DependencyProperty IsOnProperty
            = DependencyProperty.Register(nameof(IsOn),
                typeof(bool),
                typeof(ToggleSwitch),
                new PropertyMetadata(false, OnIsOnChanged));

        private static void OnIsOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ToggleSwitch)?.IsOnChanged();
        }

        public bool IsOn
        {
            get => (bool)this.GetValue(IsOnProperty);
            set => this.SetValue(IsOnProperty, value);
        }

        #endregion

        #region IsReverseColor

        public static readonly DependencyProperty IsReverseColorProperty
            = DependencyProperty.Register(nameof(IsReverseColor),
                typeof(bool),
                typeof(ToggleSwitch),
                new PropertyMetadata(true, OToggleSwitchPropChanged));

        /// <summary>
        /// 需要翻转颜色
        /// </summary>
        public bool IsReverseColor
        {
            get => (bool)this.GetValue(IsReverseColorProperty);
            set => this.SetValue(IsReverseColorProperty, value);
        }

        #endregion

        public ToggleSwitch()
        {
            InitializeComponent();
            this.IsEnabledChanged += (s, e) => { Refresh(); };
            this.SizeChanged += ToggleSwitch_SizeChanged;
        }

        private void ToggleSwitch_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            CtrlBg.CornerRadius = new CornerRadius(e.NewSize.Height / 2);
            CtrlCycle.Width = CtrlCycle.Height = e.NewSize.Height - 4;
        }

        private void IsOnChanged()
        {
            if (!this.IsLoaded) return;

            var sb = new Storyboard();
            var ta = new ThicknessAnimation();
            ta.BeginTime = new TimeSpan(0);
            ta.From = CtrlCycle.Margin;
            Thickness toMargin = new Thickness(0);
            if (IsOn)
            {
                var left = this.ActualWidth - CtrlCycle.Width - 2;
                toMargin = new Thickness(left, 2, 2, 2);
            }
            else
            {
                toMargin = new Thickness(2);
            }

            ta.To = toMargin;
            ta.Duration = new Duration(TimeSpan.FromSeconds(0.125));
            sb.Children.Add(ta);

            Storyboard.SetTarget(ta, CtrlCycle);
            Storyboard.SetTargetProperty(ta, new PropertyPath(MarginProperty));
            sb.Completed += (s, e) =>
            {
                var t = Thread.CurrentThread;
                this.Refresh();
            };
            sb.Begin(this);
        }

        private void Refresh()
        {
            if (!IsEnabled)
            {
                CtrlBg.Background = ThemeHelper.Brushes.ForegroundDisabled.Value;
                return;
            }

            Brush bg, cycle;

            if (IsOn)
            {
                bg = IsReverseColor ? Brushes.White : ThemeHelper.Brushes.AccentBase.Value;
                cycle = IsReverseColor ? ThemeHelper.Brushes.AccentBase.Value : Brushes.White;
            }
            else
            {
                bg = IsReverseColor ? Brushes.White : Brushes.LightGray;
                cycle = IsReverseColor ? Brushes.LightGray : Brushes.White;
            }

            CtrlBg.Background = bg;
            CtrlCycle.Fill = cycle;
        }
    }
}